package com.security.demo;

import com.tencent.kona.crypto.KonaCryptoProvider;
import com.tencent.kona.crypto.spec.SM2PrivateKeySpec;
import com.tencent.kona.crypto.spec.SM2PublicKeySpec;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Base64;

public class TencentEN {

    static {
        Security.addProvider(new KonaCryptoProvider());
    }


    public static void main(String[] args) throws Exception {
        TencentEN t = new TencentEN();
//        t.gen();
        String publicKeyStr = "BKptuMM0y+ZYZpzE6C4pz8TtuQv/j24CIGxlvAYWvXp4LcQYohkcfxlA+l/6fabxtmR/szgn+Po2ONJ+kGOSJFU=";
        String privateKeyStr = "EZlmKmpXUv0HOpvFTKM4PWfmOdUUEkMeQ2JdH9gxB0c=";
        byte[] encodedPublicKey = Base64.getDecoder().decode(publicKeyStr);
        byte[] encodedPrivateKey = Base64.getDecoder().decode(privateKeyStr);
        System.out.println("公钥Base64="+ publicKeyStr);
        System.out.println("私钥Base64="+ privateKeyStr);
//        System.out.println("公钥Hex="+ Hex.toHexString(encodedPublicKey));
//        System.out.println("私钥Hex="+ Hex.toHexString(encodedPrivateKey));
        System.out.println("去04公钥Base64="+ Base64.getEncoder().encodeToString(Hex.decode(Hex.toHexString(encodedPublicKey).substring(2))));

        KeyFactory keyFactory = KeyFactory.getInstance("SM2");

        SM2PublicKeySpec publicKeySpec = new SM2PublicKeySpec(encodedPublicKey);
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

        SM2PrivateKeySpec privateKeySpec = new SM2PrivateKeySpec(encodedPrivateKey);
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

        String ciphertext = "Hello";
        Cipher cipher = Cipher.getInstance("SM2");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] cleartext = cipher.doFinal(ciphertext.getBytes(StandardCharsets.US_ASCII));
        String jiamijieguo = Base64.getEncoder().encodeToString(cleartext);
//        jiamijieguo = "THOvwFAdJBTPG97dxPk1a1Ja9kH5QpB7zJdx/1zcw5pgHjIUHnRzTWIEXjZHKWK5YzSRWc4VbXA5JWzwnsawGHi3qK4keuhnlHfc4Wvtgk0sCgovdmvrHcwqvF/Kun47d9twxSA=";
        System.out.println("密文Base64："+jiamijieguo);
        System.out.println("密文Hex："+Hex.toHexString(cleartext));

        // 传入密文生成明文。
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decodetext = cipher.doFinal(cleartext);
        System.out.println("解密Base64："+ new String(decodetext,StandardCharsets.US_ASCII));



    }


    public void enctrypt(String txt) throws Exception{

    }

    public void gen() throws Exception{
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("SM2");
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
        ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
        System.out.println("公钥Base64:"+Base64.getEncoder().encodeToString(publicKey.getEncoded()));
        System.out.println("私钥Base64:"+Base64.getEncoder().encodeToString(privateKey.getEncoded()));
    }
}
